
library(plyr)
library(tidyverse)
library(car)


setwd("C:/Users/simda81/OneDrive - Linköpings universitet/Other/Effects of parliamentarianism/Onpartyorg")


Rdatapost<-read.csv("Rdata_post.csv")


#Create four-component party-index variables through the cumulative distribution function

#noporg
cumdistfct <- ecdf(Rdatapost$v2pscohesv_ols+Rdatapost$v2psplats_ols+Rdatapost$v2psprbrch_ols+
                     Rdatapost$v2psprlnks_ols) # To create the function with the variables
probs = cumdistfct(as.numeric(na.omit(Rdatapost$v2pscohesv_ols+Rdatapost$v2psplats_ols+Rdatapost$v2psprbrch_ols+
                                        Rdatapost$v2psprlnks_ols))) # To apply it to the variables

orig <- c(Rdatapost$v2pscohesv_ols+Rdatapost$v2psplats_ols+Rdatapost$v2psprbrch_ols+
            Rdatapost$v2psprlnks_ols)

#Putting the non-availables back in
rena <- function(na_vec, non_na_vec) {
  na_vec[which(!is.na(na_vec))] = non_na_vec
  na_vec
}

Rdatapost$noporg<- rena(orig,probs)


#nopcohesion
cumdistfct <- ecdf(Rdatapost$v2psorgs_ols+Rdatapost$v2psplats_ols+Rdatapost$v2psprbrch_ols+
                     Rdatapost$v2psprlnks_ols) # To create the function with the variables
probs = cumdistfct(as.numeric(na.omit(Rdatapost$v2psorgs_ols+Rdatapost$v2psplats_ols+Rdatapost$v2psprbrch_ols+
                                        Rdatapost$v2psprlnks_ols))) # To apply it to the variables

orig <- c(Rdatapost$v2psorgs_ols+Rdatapost$v2psplats_ols+Rdatapost$v2psprbrch_ols+
            Rdatapost$v2psprlnks_ols)

#Putting the non-availables back in
rena <- function(na_vec, non_na_vec) {
  na_vec[which(!is.na(na_vec))] = non_na_vec
  na_vec
}

Rdatapost$nopcohesion<- rena(orig,probs)

#nopbranches
cumdistfct <- ecdf(Rdatapost$v2psorgs_ols+Rdatapost$v2psplats_ols+Rdatapost$v2pscohesv_ols+
                     Rdatapost$v2psprlnks_ols) # To create the function with the variables
probs = cumdistfct(as.numeric(na.omit(Rdatapost$v2psorgs_ols+Rdatapost$v2psplats_ols+Rdatapost$v2pscohesv_ols+
                                        Rdatapost$v2psprlnks_ols))) # To apply it to the variables

orig <- c(Rdatapost$v2psorgs_ols+Rdatapost$v2psplats_ols+Rdatapost$v2pscohesv_ols+
            Rdatapost$v2psprlnks_ols)

#Putting the non-availables back in
rena <- function(na_vec, non_na_vec) {
  na_vec[which(!is.na(na_vec))] = non_na_vec
  na_vec
}

Rdatapost$nopbranches<- rena(orig,probs)


#nopplats
cumdistfct <- ecdf(Rdatapost$v2psorgs_ols+Rdatapost$v2psprbrch_ols+Rdatapost$v2pscohesv_ols+
                     Rdatapost$v2psprlnks_ols) # To create the function with the variables
probs = cumdistfct(as.numeric(na.omit(Rdatapost$v2psorgs_ols+Rdatapost$v2psprbrch_ols+Rdatapost$v2pscohesv_ols+
                                        Rdatapost$v2psprlnks_ols))) # To apply it to the variables

orig <- c(Rdatapost$v2psorgs_ols+Rdatapost$v2psprbrch_ols+Rdatapost$v2pscohesv_ols+
            Rdatapost$v2psprlnks_ols)

#Putting the non-availables back in
rena <- function(na_vec, non_na_vec) {
  na_vec[which(!is.na(na_vec))] = non_na_vec
  na_vec
}

Rdatapost$nopplats<- rena(orig,probs)

#noplinks
cumdistfct <- ecdf(Rdatapost$v2psorgs_ols+Rdatapost$v2psprbrch_ols+Rdatapost$v2pscohesv_ols+
                     Rdatapost$v2psplats_ols) # To create the function with the variables
probs = cumdistfct(as.numeric(na.omit(Rdatapost$v2psorgs_ols+Rdatapost$v2psprbrch_ols+Rdatapost$v2pscohesv_ols+
                                        Rdatapost$v2psplats_ols))) # To apply it to the variables

orig <- c(Rdatapost$v2psorgs_ols+Rdatapost$v2psprbrch_ols+Rdatapost$v2pscohesv_ols+
            Rdatapost$v2psplats_ols)

#Putting the non-availables back in
rena <- function(na_vec, non_na_vec) {
  na_vec[which(!is.na(na_vec))] = non_na_vec
  na_vec
}

Rdatapost$noplinks<- rena(orig,probs)



Rdatapost<-rename(Rdatapost, partyinst = v2xps_party_ols)

Rdatapost<-filter(Rdatapost, year>1788)

#Decade dummies

Rdatapost$decade<-factor(with(Rdatapost, ifelse(year == 1789, 1780,ifelse(year> 1789 & year < 1800, 1790,
                                                                    ifelse(year> 1799 & year < 1810, 1800,
                                                                    ifelse(year> 1809 & year < 1820, 1810,
                                                                    ifelse(year> 1819 & year < 1830, 1820,
                                                                    ifelse(year> 1829 & year < 1840, 1830,
                                                                    ifelse(year> 1839 & year < 1850, 1840,
                                                                    ifelse(year> 1849 & year < 1860, 1850,
                                                                    ifelse(year> 1859 & year < 1870, 1860,
                                                                    ifelse(year> 1869 & year < 1880, 1870,
                                                                    ifelse(year> 1879 & year < 1890, 1880,
                                                                    ifelse(year> 1889 & year < 1900, 1890,
                                                                    ifelse(year> 1899 & year < 1910, 1900,
                                                                    ifelse(year> 1909 & year < 1920, 1910,
                                                                    ifelse(year> 1919 & year < 1930, 1920,
                                                                    ifelse(year> 1929 & year < 1940, 1930,
                                                                    ifelse(year> 1939 & year < 1950, 1940,
                                                                    ifelse(year> 1949 & year < 1960, 1950,
                                                                    ifelse(year> 1959 & year < 1970, 1960,
                                                                    ifelse(year> 1969 & year < 1980, 1970,
                                                                    ifelse(year> 1979 & year < 1990, 1980,
                                                                    ifelse(year> 1989 & year < 2000, 1990,
                                                                    ifelse(year> 1999 & year < 2010, 2000,
                                                                    ifelse(year> 2009 & year < 2020, 2010,
                                                                    ifelse(year> 2019 & year < 2030, 2020, NA)))))))))))))))))))))))))))


#Factor variable, PR/not PR

Rdatapost$PR<-factor(with(Rdatapost, ifelse(e_s == "maj" | e_s == "mixed", "Not PR", ifelse(e_s == "pr", "PR", "NA"))))

#Retrieve V-Dem civil-Society data

vdem<-readRDS("V-Dem-CY-Full+Others-v10.rds")

vdem2 <- dplyr::select(vdem, country_name, year, v2xcs_ccsi)

vdem2<-vdem2 %>% mutate(country = case_when(
  country_name == "United Kingdom"~"Britain",
  country_name == "Piedmont-Sardinia"~"Italy",
  TRUE~country_name
))

vdem2<-vdem2[!(vdem2$country=="Italy" & vdem2$year == 1861 & vdem2$v2xcs_ccsi == 0.252),]




Rdatapost<-left_join(Rdatapost, vdem2)

remove(vdem, vdem2)



Rdatapost<-rename(Rdatapost, cs= v2xcs_ccsi)

##CS conditions data from Ebbinghaus (1995)


Rdatapost$fropress<-factor(with(Rdatapost, ifelse(country == "Austria" & year > 1914 |
                                                  country == "Belgium" & year > 1829 |
                                                  country == "Britain" & year > 1829 |
                                                  country == "Denmark" & year > 1845 | 
                                                  country == "Finland" & year > 1914 | 
                                                  country == "France" & year > 1880 |
                                                  country == "Germany" & year > 1914 |
                                                  country == "Italy" & year > 1899 |
                                                  country == "Netherlands" & year > 1847 |
                                                  country == "Norway" & year > 1813 |
                                                  country == "Sweden" & year > 1837, 1, 0)))
  
Rdatapost$froass<-factor(with(Rdatapost, ifelse(country == "Austria" & year > 1866 |
                                                    country == "Belgium" & year > 1829 |
                                                    country == "Britain" & year > 1823 |
                                                    country == "Denmark" & year > 1848 | 
                                                    country == "Finland" & year > 1905 | 
                                                    country == "France" & year > 1883 |
                                                    country == "Germany" & year > 1868 |
                                                    country == "Italy" & year > 1889 |
                                                    country == "Netherlands" & year > 1854 |
                                                    country == "Norway" & year > 1838 |
                                                    country == "Sweden" & year > 1863, 1, 0)))
  
Rdatapost$frocomb<-factor(with(Rdatapost, ifelse(country == "Austria" & year > 1869 |
                                                      country == "Belgium" & year > 1865 |
                                                      country == "Britain" & year > 1823 |
                                                      country == "Denmark" & year > 1848 | 
                                                      country == "Finland" & year > 1905 | 
                                                      country == "France" & year > 1883 |
                                                      country == "Germany" & year > 1868 |
                                                      country == "Italy" & year > 1889 |
                                                      country == "Netherlands" & year > 1854 |
                                                      country == "Norway" & year > 1838 |
                                                      country == "Sweden" & year > 1863, 1, 0)))

Rdatapost$froass2<-as.numeric(Rdatapost$froass)-1
Rdatapost$frocomb2<-as.numeric(Rdatapost$frocomb)-1
Rdatapost$fropress2<-as.numeric(Rdatapost$fropress)-1


Rdatapost$freedoms=((Rdatapost$froass2)+(Rdatapost$frocomb2)+(Rdatapost$fropress2))/3


#Calculate correlations and make graphs for the relevant period 
parldata_prewar2 <- filter(Rdatapost, year > 1869 & year < 1940)

#The correlations for the manuscript, 2024-11-21
library(rrtable)
library(flextable)
library(officer)


dataforcorr2 <- parldata_prewar2 %>% dplyr::select(partyinst, prule, suffrage, cs) #Correlations used. Right period. 
df.corr <- psych::corr.test(dataforcorr2, adjust = "none")

inter_corr_r <- (df.corr$stars)
inter_corr_r <- cbind(var = rownames(inter_corr_r), inter_corr_r)
inter_corr_r <- as.data.frame(inter_corr_r)
inter_corr_r[upper.tri(inter_corr_r)] <- NA

ft.corr <- flextable(inter_corr_r) |> 
  set_header_labels(var = "")  |> 
  set_caption("Table 1: Correlations, 1870-1930")  

centered_text <- fpar(ftext("*** indicates p-values below 0.001.", prop = fp_text()), fp_p = fp_par(text.align = "center"))

doc2 <- read_docx() %>%
  body_add_flextable(value = ft.corr) %>%
  body_add_fpar(value = centered_text) # Add the centered text

# Save the document
print(doc2, target = "lower_triangle_correlation_table2.docx")


###Paper graph, Figure 1



allinonenew<-ggplot(data=parldata_prewar2, aes(year)) + 
  #geom_line(aes(y=ssstotal_ols, colour = "suffrage")) + 
  geom_line(aes(y=cs, colour ="Civil Society", linetype="Civil Society")) +  
  geom_line(aes(y=prule, colour="Parliamentarism", linetype="Parliamentarism")) +
  geom_line(aes(y=suffrage, colour="Suffrage", linetype="Suffrage")) + 
  geom_line(aes(y=partyinst, colour="Party Strength",linetype="Party Strength")) + 
  facet_wrap(~country)+
  labs(title = "", x = "Year", y = "", color = "", linetype ="") + ylim(0,1)+
  scale_color_manual(
            values = c("darkgray","black","gray","lightgray")) +
 # scale_linetype_manual(
 #  values= c("solid","dashed","twodashed","dotdash"))+
  theme_bw()+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "black"))+xlab("Year")+ylab("")


allinonenew<-allinonenew + scale_x_continuous(breaks=c(1870, 1900, 1930))

allinonenew<-allinonenew + theme(legend.position =c (0.88, 0.15))

allinonenew

plot2docx(allinonenew)

#ggsave("allinonenew.pdf", width = 24, height = 16, units = "cm")

###Check graph with freedoms instead of cs
allinonenew2<-ggplot(data=parldata_prewar2, aes(year)) + 
  #geom_line(aes(y=ssstotal_ols, colour = "suffrage")) + 
  geom_line(aes(y=freedoms, colour ="Freedoms", linetype="Freedoms")) +  
  geom_line(aes(y=prule, colour="Parliamentarism", linetype="Parliamentarism")) +
  geom_line(aes(y=suffrage, colour="Suffrage", linetype="Suffrage")) + 
  geom_line(aes(y=partyinst, colour="Party Strength",linetype="Party Strength")) + 
  facet_wrap(~country)+
  labs(title = "", x = "Year", y = "", color = "", linetype ="") + ylim(0,1)+
  scale_color_manual(
    values = c("darkgray","black","gray","lightgray")) +
  # scale_linetype_manual(
  #  values= c("solid","dashed","twodashed","dotdash"))+
  theme_bw()+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "black"))+xlab("Year")+ylab("")



###Appendix graph
allinone2<-ggplot(data=Rdatapost, aes(year)) + 
  #geom_line(aes(y=ssstotal_ols, colour = "suffrage")) + 
  geom_line(aes(y=cs, colour ="Civil Society", linetype="Civil Society")) +  
  geom_line(aes(y=prule, colour="Parliamentarism", linetype="Parliamentarism")) +
  geom_line(aes(y=suffrage, colour="Suffrage", linetype="Suffrage")) + 
  geom_line(aes(y=partyinst, colour="Party Strength",linetype="Party Strength")) + 
  facet_wrap(~country)+
  labs(title = "", x = "Year", y = "", color = "", linetype ="") + ylim(0,1)+
  scale_color_manual(
    values = c("darkgray","black","gray","lightgray")) +
  # scale_linetype_manual(
  #  values= c("solid","dashed","twodashed","dotdash"))+
    theme_bw()+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "black"))+xlab("Year")+ylab("")

allinone2<-allinone2 + scale_x_continuous(breaks=c(1800, 1900, 2000))

allinone2<-allinone2 + theme(legend.position =c (0.88, 0.15))

allinone2

plot2docx(allinone2)

#ggsave("allinone2.pdf", width = 24, height = 16, units = "cm")

write.csv(Rdatapost, "statadatapost.csv", row.names=FALSE)


